/*
* Conditions Of Use
*
* This software was developed by employees of the Sigmatrix(Beijing) Corporation.
* This software is provided by sigmatrix as a service and is expressly
* provided "AS IS."  Sigmatrix MAKES NO WARRANTY OF ANY KIND, EXPRESS, IMPLIED
* OR STATUTORY, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTY OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT
* AND DATA ACCURACY.  Sigmatrix does not warrant or make any representations
* regarding the use of the software or the results thereof, including but
* not limited to the correctness, accuracy, reliability or usefulness of
* the software.
*
* Permission to use this software is contingent upon your acceptance
* of the terms of this agreement.
*
*/
package com.sigmatrix.h5.web.controller.front;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.sigmatrix.h5.auth.ILoginUser;
import com.sigmatrix.h5.config.LoginUser;
import com.sigmatrix.h5.enums.CrmAcctType;
import com.sigmatrix.h5.enums.RebateTarget;
import com.sigmatrix.h5.enums.WalletType;
import com.sigmatrix.h5.service.DisRebateDetailService;
import com.sigmatrix.h5.service.common.ServiceHelper;
import com.sigmatrix.h5.service.drawcash.IDrawCash;
import com.sigmatrix.h5.service.drawcash.IRedPackReceipt;
import com.sigmatrix.h5.utils.CommonUtils;	
import com.sigmatrix.h5.web.BaseController;
import com.sigmatrix.h5.web.Result;
import com.sigmatrix.h5.web.args.DrawCashParam;
import com.sigmatrix.h5.web.args.RebateDetailParam;
import com.sigmatrix.h5.web.mode.RebateList;
import com.sigmatrix.h5.web.mode.RebateList.Detail;
import com.sigmatrix.sc.jdbc.common.lang.Page;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;


@Api(tags="返利明细v2")
@RestController
@RequestMapping("/front/rebate")
public class RebateControllerV2 extends BaseController{
	
	@Autowired
	private DisRebateDetailService disRebateDetailService;
	
	@Autowired
	private IDrawCash drawCashHandler;
	
	@ApiOperation("查看账户收支明细")
	@PostMapping("/v2/income/expense/detail/list")
	public Result<RebateList> findRebateList(@LoginUser ILoginUser user,@RequestBody RebateDetailParam vo){
		Integer cardHolder = null;
		Short dealerType = null;
		if(CrmAcctType.isDealerAcct(user.getAcctType())){//经销商类型账户
			if(WalletType.isPersonalWallet(vo.getWalletType())){
				cardHolder = user.getAcctId();	
				dealerType = RebateTarget.SALES_MAN.value;
			}else if(WalletType.isEntWallet(vo.getWalletType())){
				cardHolder = user.getDealerOrShopId();
				dealerType = RebateTarget.DEALER.value;
			}else{
				return error("未指定零钱包类型。");
			}
		}else if(CrmAcctType.isShopAcct(user.getAcctType())){//终端店类型账户
			cardHolder = user.getDealerOrShopId();
			dealerType = RebateTarget.TERMINAL_SHOP.value;
		}else if(CrmAcctType.isEseAcct(user.getAcctType())){//企业下账户
			cardHolder = user.getAcctId();
			dealerType = RebateTarget.SALES_MAN.value;
		}else{
			return error("只有经销商账户跟终端店账户才能查看");
		}
		if(cardHolder==null){
			return error("系统错误");
		}
		
		RebateList draw = disRebateDetailService.rebateList(cardHolder, dealerType, vo.getPageNo(), vo.getPageSize());
		return convertResponse(draw, ok(draw));
	}
	
	/**
	 * 将 Result<RebateList>转换成通用的page格式
	 * 
	 * @param draw
	 * @param result
	 * @return
	 */
	private Result<RebateList> convertResponse(RebateList draw, Result<RebateList> result) {
		if (draw != null && draw.getRebateDetail() != null && result != null) {
			Page<Detail> rebateDetail = draw.getRebateDetail();
			result.setPageNo(rebateDetail.getPageNo());
			result.setPageSize(rebateDetail.getPageSize());
			result.setTotal(rebateDetail.getTotal());
		}
		return result;
	}
	
//	//TODO 替换老的接口。未使用未测
//	@ApiOperation("微信红包提现")
//	@PostMapping("/v2/wechat/redpack/handleCash")
//	public Result<Object> withDrawCash(@LoginUser ILoginUser user, @RequestBody DrawCashParam vo){
//		
//		ServiceHelper.selectSmsService(null).validateCode(vo.getSmsCode());
//		if(CommonUtils.isEmpty(vo.getOpenId())|| "null".equals(vo.getOpenId())){
//			return error("openId为空");
//		}
//		IRedPackReceipt redPackReceipt = drawCashHandler.sendWeChatRedPack(user, vo);
//		return Result.ok(redPackReceipt);
//	}
	
}
